package com.youkeyi.ddy.cloud.application.business.crm.config.controller;

import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.BusinessCustomerConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.dto.BusinessCustomerConfigListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.dto.BusinessCustomerConfigListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.param.BusinessCustomerConfigListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.business.crm.config.param.BusinessCustomerConfigUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.CrmCustomerConfigCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.dto.CrmCustomerConfigListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerConfigQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.crm.config.param.CrmCustomerConfigUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * BusinessCustomerConfigController
 *
 * @Author liuxiawang
 * @Date 2024/4/1 3:31 PM
 * @Location shenzhen.china
 */
@Service
public class BusinessCustomerConfigController implements BusinessCustomerConfigCloudService {

    @Autowired
    private CrmCustomerConfigCloudService crmCustomerConfigCloudService;

    @Override
    public BusinessCustomerConfigListCloudDto list(BusinessCustomerConfigListCloudParam param) {
        BusinessCustomerConfigListCloudDto dto = new BusinessCustomerConfigListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        CrmCustomerConfigQueryCloudParam configQueryCloudParam = new CrmCustomerConfigQueryCloudParam();
        configQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        configQueryCloudParam.setPageSize(param.getPageSize());
        configQueryCloudParam.setCurrentPage(param.getCurrentPage());
        CrmCustomerConfigListCloudDto configDtos = crmCustomerConfigCloudService.query(configQueryCloudParam);
        if (null != configDtos && CollectionUtils.isNotEmpty(configDtos.getRows())) {
            List<BusinessCustomerConfigListItemCloudDto> items = new ArrayList<>();
            configDtos.getRows().forEach(config -> {
                BusinessCustomerConfigListItemCloudDto item = new BusinessCustomerConfigListItemCloudDto();
                item.setCustomerConfigPrimaryKey(config.getCustomerConfigPrimaryKey());
                item.setCorpKey(config.getCorpKey());
                item.setIsOpen(config.getIsOpen());
                item.setReleaseFrequently(config.getReleaseFrequently());
                item.setRemark(config.getRemark());
                item.setIsDeleted(config.getIsDeleted());
                item.setIsDeletedName(config.getIsDeletedName());
                item.setCreateUserKey(config.getCreateUserKey());
                item.setCreateUserName(config.getCreateUserName());
                item.setCreateTime(config.getCreateTime());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(configDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent update(BusinessCustomerConfigUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getCustomerConfigPrimaryKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        CrmCustomerConfigUpdateCloudParam configUpdateCloudParam = new CrmCustomerConfigUpdateCloudParam();
        configUpdateCloudParam.setCustomerConfigPrimaryKey(param.getCustomerConfigPrimaryKey());
        configUpdateCloudParam.setIsOpen(param.getIsOpen());
        configUpdateCloudParam.setReleaseFrequently(param.getReleaseFrequently());
        configUpdateCloudParam.setRemark(param.getRemark());
        configUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        configUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return crmCustomerConfigCloudService.update(configUpdateCloudParam);
    }

}
